package edu.hubu.CollectionTraverse;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListTraverse {
    public static void main(String[] args) {
        //TODO:1.创建ArrayList对象
        List<Integer> list = new ArrayList<>();

        //TODO:2.添加数据
        list.add(1);
        list.add(12);
        list.add(9);
        list.add(25);
        list.add(6);

        //TODO:3.采用Iterator迭代器遍历
        System.out.println("采用Iterator迭代器循环遍历ArrayList集合:");
        Iterator<Integer> it = list.iterator();
        while (it.hasNext()) {
            Integer next = it.next();
            System.out.print(next + ", ");
        }
        System.out.println();

        //TODO:4.采用传统for循环遍历
        System.out.println("采用传统for循环遍历ArrayList集合:");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + ", ");
        }
        System.out.println();

        //TODO:5.采用增强for循环遍历
        System.out.println("采用增强for循环遍历ArrayList集合:");
        for (int a : list) {
            System.out.print(a + ", ");
        }
        System.out.println();
    }
}
